package com.fr.design.plugin.sap;

import com.fr.data.impl.sap.SAPTransfer;
import com.fr.data.impl.sap.SAPTransferManager;
import com.fr.design.gui.controlpane.JControlPane;
import com.fr.design.gui.controlpane.NameObjectCreator;
import com.fr.design.gui.controlpane.NameableCreator;
import com.fr.general.Inter;
import com.fr.general.NameObject;
import com.fr.stable.Nameable;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class SAPSyncPane extends JControlPane {

	@Override
	protected String title4PopupWindow() {
		return Inter.getLocText("ServerM-SapSync_Manager");
	}

	@Override
	public NameableCreator[] createNameableCreators() {
		return new NameableCreator[]{new NameObjectCreator(
				Inter.getLocText("Syn-transfer"),
				"/com/fr/design/images/m_web/sync.png",
				SAPTransfer.class,
				SAPTransferPane.class
		)};
	}
	
	public void populate(SAPTransferManager transferManager) {
		if (transferManager == null) {
			return;
		}

		List nameTransferList = new ArrayList();

		Iterator transferNameIterator = transferManager.getSAPTransferNameIterator();
		while (transferNameIterator.hasNext()) {
			String name = (String)transferNameIterator.next();
			SAPTransfer transfer = transferManager.getSAPTransfer(name);

			if (transfer != null) {
				nameTransferList.add(new NameObject(name, transfer));
			}
		}

		NameObject[] nameObjects = new NameObject[nameTransferList.size()];
		nameTransferList.toArray(nameObjects);

		populate(nameObjects);
	}
	
	public void update(SAPTransferManager transferManager) {
		transferManager.clearAllSAPTransfer();

		Nameable[] nameables = this.update();

		for (int i = 0; i < nameables.length; i++) {
			transferManager.putSAPTransfer(((NameObject)nameables[i]).getName(), (SAPTransfer)((NameObject)nameables[i]).getObject());
		}
	}
}
